<?php

/**
 * pagina actions.
 *
 * @package    Terapia Vital
 * @subpackage pagina
 * @author     Foxsis
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class paginaActions extends sfActionsFoxsis
{
  public function executeIndex(sfWebRequest $request)
  {
    return $this->forward('pagina', 'list');
  }

	public function executeGrid()
	{
		//Recupero todas as informações necessárias para trabalhar com o grid
		$this->getRequestDataTable();
		$this->data					= PaginaTable::consultar($this->filter, $this->order);
		$this->total_data			= count($this->data);
		$this->data					= $this->paginadorDataTable($this->data, $this->page_num, parent::LIMIT_PAGE_DATA_TABLE);

		sfLoader::loadHelpers(array('Url'));

	    foreach($this->data as $key => &$r)
	    {
	    	$r['created_at'] = format_datetime($r['created_at'], 'p');
	    	$r['updated_at'] = format_datetime($r['updated_at'], 'p');
	    	$r['st_pagina'] = ($r['st_pagina'] == 'S') ? 'SIM' : 'NÃO';
	    	$r['tp_pagina'] = Pagina::getTipo($r['tp_pagina']);
	    	
	    	$actions = array(
				'edit'	 => "<a href='".url_for($this->getModuleName().'/edit?id_pagina='.$r['id_pagina'])."' class='icoEditar'></a>",
				'delete' => "<a href='".url_for($this->getModuleName().'/delete?id_pagina='.$r['id_pagina'])."'  class='icoExcluir delete'></a>",
			);
			$this->data[$key]['act'] = implode(' ', $actions);
    	}

		$this->setJSONDataTable($this->data, $this->colunas, $this->total_data);
		return sfView::NONE;
	}


    public function executeList()
    {
        $this->form = new PaginaForm();
    	$this->colunas	= array(
          	$this->form['id_pagina']->renderLabel() => "id_pagina",
      		$this->form['tp_pagina']->renderLabel() => "tp_pagina",
      		$this->form['ds_titulo']->renderLabel() => "ds_titulo",
      		$this->form['st_pagina']->renderLabel() => "st_pagina",
      		$this->form['created_at']->renderLabel() => "created_at",
      		$this->form['updated_at']->renderLabel() => "updated_at",
			"Ações"	=> "act"
		);

        $this->filter = $this->_getFilter();

        $this->setTemplate('index');
    }

    public function executeFilter()
	{
	    $filter = new Filtro(
			$this->getRequestParameter(sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
			sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);

		return $this->forward($this->getModuleName(), '/list');
	}

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PaginaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PaginaForm();

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'new');
  }

  public function validateCreate()
  {
  	return true;
  }

  public function handleErrorCreate()
  {
    return $this->forward($this->getModuleName(), 'new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($pagina = Doctrine::getTable('Pagina')->find(array($request->getParameter('id_pagina'))), sprintf('Object pagina does not exist (%s).', $request->getParameter('id_pagina')));
    $this->form = new PaginaForm($pagina);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($pagina = Doctrine::getTable('Pagina')->find(array($request->getParameter('id_pagina'))), sprintf('Object pagina does not exist (%s).', $request->getParameter('id_pagina')));
    $this->form = new PaginaForm($pagina);

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'edit');
  }

  public function validateUpdate()
  {
  	return true;
  }

  public function handleErrorUpdate()
  {
    return $this->forward($this->getModuleName(), 'edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    try
    {
    $request->checkCSRFProtection();

    $this->forward404Unless($pagina = Doctrine::getTable('Pagina')->find(array($request->getParameter('id_pagina'))), sprintf('Object pagina does not exist (%s).', $request->getParameter('id_pagina')));
    $pagina->delete();

    $this->redirect('pagina/index');
    }
    catch(Excepiton $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
    	$this->redirect('pagina/index');
	}

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	try
  	{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $pagina = $form->save();
	  $this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
      $this->redirect('pagina/edit?id_pagina='.$pagina->getIdPagina());
    }
    else
    {
    throw new Exception($form->getErrorSchema());
    }
    }
    catch(Exception $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
	}
  }
}
